const {advertisingModel}=require('../modules/advertisingModel');

// 新增广告位
async function addAdvertising(req, res, next){
    
    const data= req.body;
    await advertisingModel.create(data);
    res.send({
        message:'广告位新增成功',
        code:200,
        
    })
}

// 获取广告位数据
async function getAdvertising(req,res,next){
    const {value}=req.query;
    console.log(value);
    const result=await advertisingModel.find({
        $or: [
            { advertisingNumber: { $regex: value, $options: '$i' } },
            { advertisingName: { $regex: value, $options: '$i' } },
            
        ]
    })
    .populate('community_id') ;
    // console.log(result);
    res.send({
        message:'车位数据获取成功',
        code:200,
        result
    })
    



}

// 删除
async function deleteAdvertising(req,res,next){
    const _id=req.body;
    await advertisingModel.deleteOne({_id});
    res.send({
        message:'删除数据成功',
        code:200
    })

}



// 传入一个小区id，获取这个小区下面的所有的广告位
async function getadvertisingByID(req,res,next){
    const community_id=req.params;
    const result=await advertisingModel.find({community_id})
    res.send({
        message:'获取广告位成功',
        code:200,
        result
    })
}







module.exports={addAdvertising,getAdvertising,deleteAdvertising,getadvertisingByID}